﻿using System;
using System.Linq;
using ActionGraph.Core;
using ActionGraph.CustomNode;
using UnityEngine;
using XNode;
using XNodeEditor;

namespace Actions.Editor
{
    [CustomNodeGraphEditor (typeof(ActionGraph.Core.ActionGraph))]
    public class ActionGraphEditor : NodeGraphEditor
    {
        private ActionGraph.Core.ActionGraph _actionGraph => target as ActionGraph.Core.ActionGraph;
        
        public override string GetNodeMenuName (Type type)
        {
            if (type.CustomAttributes != null
             && type.CustomAttributes.Select (data => data.AttributeType == typeof(NodeHideInCreateMenuAttribute))
                    .Count () != 0)
                return null;

            if (type.FullName != null && type.FullName.Contains ("Actions.Core"))
                return null;

            if (type.IsSubclassOf (typeof(ActionNode)) || type.IsAssignableFrom (typeof(ActionNode)))
            {
                return "ActionNode/" + base.GetNodeMenuName (type).Replace ("Actions/Custom Node/", "");
            }

            return null;
        }
        public override Node CreateNode (Type type, Vector2 position)
        {
            var node = base.CreateNode (type, position);

            if (type == typeof(EntryNode))
            {
                _actionGraph.entryNode = node as ActionNode;
            }
            else if (type == typeof(UpdateNode))
            {
                _actionGraph.updateNode = node as ActionNode;
            }

            return node;
        }
    }
}